package com.wx_shop.api.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.wx_shop.api.comm.ServiceCode;
import com.wx_shop.api.dao.MiniProgramDao;
import com.wx_shop.api.service.UserService;
import com.wx_shop.api.util.JWTHelperUtil;
import com.wx_shop.api.util.wx_mini_program.MiniProgramUtil;
import com.wx_shop.api.vo.BaseRes;
import com.wx_shop.api.vo.UserVo;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private MiniProgramUtil miniProgramUtil;

	@Resource(name="MiniProgramDao_DB")
	private MiniProgramDao miniProgramDao;
	
	@Resource
	private JWTHelperUtil jWTHelperUtil;
	
	@Override
	public BaseRes<String> miniProgramLogin(String code, String encryptedData) {
		
		UserVo userVo = new UserVo();
		userVo.setId(1);
		userVo.setNickName("小明");
		
		String token = jWTHelperUtil.getToken(userVo);
		
		/**
		LoginResEntity rLoginResEntity = miniProgramUtil.jscode2session(code);
		if( rLoginResEntity == null ) {
			
		}
		miniProgramDao.saveOrRefresh(rLoginResEntity);
		*/
		
		BaseRes<String> res = new BaseRes<String>();
		res.setData(token);
		res.setServiceCode(ServiceCode.SUCCESS);
		return res;
	}

	@Override
	public BaseRes<UserVo> userInfo() {
		
		UserVo userVo = (UserVo) SecurityContextHolder.getContext().getAuthentication().getDetails();
		
		BaseRes<UserVo> res = new BaseRes<UserVo>();
		res.setData(userVo);
		res.setServiceCode(ServiceCode.SUCCESS);
		return res;
	}
	
}
